<template>
	<section>
		<!--查询工具条-->
		<el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
			<el-form :inline="true" :model="filters">
				<el-form-item>
					<el-input v-model="filters.name" placeholder="部门名称"></el-input>
				</el-form-item>
				<el-form-item>
					<el-button type="primary" v-on:click="loadListData">查询</el-button>
				</el-form-item>
				<el-form-item>
					<el-button type="primary" @click="handleAdd">新增</el-button>
				</el-form-item>
			</el-form>
		</el-col>
		<!--列表展示-->
		<el-table :data="departments" highlight-current-row v-loading="listLoading" @selection-change="selsChange" style="width: 100%;">
			<el-table-column type="selection" width="55">
			</el-table-column>
			<el-table-column type="index" width="60">
			</el-table-column>
			<el-table-column prop="name" label="部门名称" width="120">
			</el-table-column>
			<el-table-column prop="sn" label="编号" width="100"  >
			</el-table-column>
			<el-table-column prop="dirPath" label="路径" width="100" >
			</el-table-column>
			<el-table-column prop="state" label="状态" width="120" >
				<template scope="scope">
					<span style="color: #11b95c" v-if="scope.row.state===0">启用</span>
					<span style="color: red" v-else-if="scope.row.state===-1">禁用</span>
					<span style="color: plum" v-else>未知</span>
				</template>
			</el-table-column>
			<el-table-column prop="manager.username" label="管理员" min-width="180" >
			</el-table-column>

			<el-table-column prop="parent.name" label="上级部门" min-width="180" >
			</el-table-column>
			<el-table-column label="操作" width="150">
				<template scope="scope">
					<el-button size="small" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
					<el-button type="danger" size="small" @click="handleDel(scope.$index, scope.row)">删除</el-button>
				</template>
			</el-table-column>
		</el-table>
		<!--工具条-->
		<el-col :span="24" class="toolbar">
			<el-button type="danger" @click="batchRemove" :disabled="this.sels.length===0">批量删除</el-button>
			<el-pagination layout="prev, pager, next" @current-change="handleCurrentChange" :page-size="pageSize" :total="total" style="float:right;">
			</el-pagination>
		</el-col>
		<!--新增界面-->
		<el-dialog title="title"  :visible.sync="addFormVisible" :close-on-click-modal="false">
			<el-form :model="addForm" label-width="80px" :rules="addFormRules" ref="addForm">
				<el-form-item label="部门名称" prop="name">
					<el-input v-model="addForm.name" auto-complete="off"></el-input>
				</el-form-item>
				<el-form-item label="部门编号">
					<el-input v-model="addForm.sn"></el-input>
				</el-form-item>
				<el-form-item label="状态">
					<el-radio-group v-model="addForm.state">
						<el-radio class="radio" :label="0">启用</el-radio>
						<el-radio class="radio" :label="-1">禁用</el-radio>
					</el-radio-group>
				</el-form-item>
				<el-form-item label="部门经理">

					<el-select v-model="addForm.manager" value-key="id" placeholder="请选择">
						<el-option
								v-for="item in emps"
								:key="item.id"
								:label="item.username"
								:value="item">
							<span style="float: left">{{ item.username }}</span>
							<span style="float: right; color: #8492a6; font-size: 13px">{{ item.phone }}</span>
						</el-option>
					</el-select>
				</el-form-item>
				<el-form-item label="上级部门">

					<el-cascader
							v-model="addForm.parent"
							:options="deptTree"
							:props="{
								checkStrictly: true,
								 value:'id',
								 label:'name',

								 }"
							clearable></el-cascader>
				</el-form-item>
			</el-form>
			<div slot="footer" class="dialog-footer">
				<el-button @click.native="addFormVisible = false">取消</el-button>
				<el-button type="primary" @click.native="addSubmit" :loading="addLoading">提交</el-button>
			</div>
		</el-dialog>
	</section>
</template>

<script>
    export default {
        data() {
            // 用户模版双向绑定
            return {
                emps:[],
				deptTree:[],
                listLoading:false,
                departments:[], //展示的数据列表--数据源
                sels:[],//列表选中列
				pageSize:5,
                total:0,
				currentPage:1,
                filters:{
                    name:''
				},
                addFormVisible: false,//新增界面是否显示
                addLoading: false,
                addFormRules: {
                    name: [
                        { required: true, message: '请输入姓名', trigger: 'blur' }
                    ]
                },
                //新增界面数据
                addForm: {
                    id:null,
                    name: '',
                    sn: '',
                    state: 0,
                    parent: null,
                    manager: null
                },

                title:'新增',


            }
        },
        // 用来定义方法的集合
        methods: {
            //当选择项发生变化时会触发该事件
            selsChange: function (sels) {
                this.sels = sels;
            },
            //编辑方法
            handleEdit(i,row){
                this.title='编辑';
                this.addFormVisible = true;
                let para = Object.assign(this.addForm,row);

                let arr =this.addForm.dirPath.split("/");
				this.addForm.parent = arr.slice(1,arr.length-1).map(Number);
				console.log(parent)
                this.getDeptTree();

			},
            //删除的方法
            handleDel(i,row){
                this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    let id = row.id;
                    this.$http.delete("/dept/"+id).then(res=>{
                        if(res.data.success){
                            this.$message({
                                type: 'success',
                                message: res.data.message
                            });
						}else {
                            this.$message({
                                type: 'error',
                                message: res.data.message
                            });

						}
                        this.loadListData();
					})

                }).catch(() => {
                    this.$message({
                        type: 'info',
                        message: '已取消删除'
                    });
                });

			},
            //批量删除绑定方法
            batchRemove(){
                this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(()=>{
                    let ids = this.sels.map(item=>item.id);
                    this.$http.patch("/dept",ids).then(res=>{
                        if(res.data.success){
                            this.$message({
                                type: 'success',
                                message: res.data.message
                            });
                        }else {
                            this.$message({
                                type: 'error',
                                message: res.data.message
                            });

                        }
                        this.loadListData();
                    })

                }).catch(() => {
                    this.$message({
                        type: 'info',
                        message: '已取消删除'
                    });
                });




			},
			//新增方法
            handleAdd(){
                this.addForm= {
                    id:null,
					name: '',
					sn: '',
					state: 0,
					parent: null,
					manager: null
                },
                this.addFormVisible=true;
                this.getDeptTree();
			},
            addSubmit(){
                this.$refs.addForm.validate((valid) => {
                    if(valid){
                        this.$confirm('确认提交吗？','提示',{}).then(()=>{
							this.addLoading = true;
							let para = Object.assign({},this.addForm);
							let parent = {"id":para.parent[para.parent.length-1]}
                            para.parent = parent;
							this.$http.put("/dept",para).then(res=>{
								if(res.data.success){
									this.$message({
										type: 'success',
										message: res.data.message
									});
									this.loadListData();
								}else {
									this.$message({
										type: 'error',
										message: res.data.message
									});

								}

							})
							this.addFormVisible=false;
							this.addLoading=false;
                    	})
					}
				})
			},
            handleCurrentChange(page){
                this.currentPage = page;
                this.loadListData();

			},
            loadListData(){
                // 页面加载完毕以后 立即 请求后台获取数据
                /*
                    1.axios发送请求

                    2.获取到数据的集合
                    3.给departments赋值
                 */
                let param = {
                    currentPage:this.currentPage,
                    pageSize:5,
                    keyword:this.filters.name
                }
                this.$http.post("/dept",param).then(res=>{
                    //res  请求返回的数据
                    this.departments = res.data.rows;
                    this.total = res.data.total;

                })
			},
            loadEmps() {
                this.$http.get("/emp").then(res => {
                    this.emps = res.data;
                })
            },
            getDeptTree() {
                this.$http.get("/dept/deptTree").then(res => {
                    this.deptTree = res.data;
                })
            }



		},
        //钩子方法
        mounted() {// 页面加载完成以后 立即执行里面的内容
            this.loadListData();
            this.loadEmps();


        }
    }

</script>

<style scoped>

</style>